﻿using GKQC.Common;
using GKQC.DbHelper.Dao;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.ErpDbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GKQC.DbHelper.Services
{
    /// <summary>
    /// ERP检测项目代号与检测系统代号对照表
    /// </summary>
    public class ItemCodeTranService
    {
        public static void GetItemCodeTran(AllDbContext dbContext, List<ItemCodeTran> list)
        {
            if (list == null) list = new List<ItemCodeTran>();
            try
            {
                List<ItemCodeTran> trans = dbContext.QcDb.Queryable<ItemCodeTran>().ToList();
                if (trans != null && trans.Any())
                {
                    list.Clear();
                    list.AddRange(trans);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        public static void TranItemCodeFromErp(List<ItemCodeTran> itemCodeTrans, List<qcd_view> qcd_Views)
        {
            if (itemCodeTrans == null || qcd_Views == null) return;
            if (itemCodeTrans.Count == 0 || qcd_Views.Count == 0) return;

            qcd_Views.ForEach(q =>
            {
                var tran = itemCodeTrans.Where(t => t.ErpItemCode == q.qcd02).FirstOrDefault();
                if (tran != null)
                {
                    q.ItemCode = tran.ItemCode;
                }
            });
        }
    }
}
